//
//  YFLoginApi.m
//  CoollangTennisBall
//
//  Created by Coollang on 2018/1/12.
//  Copyright © 2018年 Coollang-YF. All rights reserved.
//

#import "YFLoginApi.h"

@implementation YFLoginApi

// 刷新登录
+ (instancetype)refreshLoginApi {
    return [YFLoginApi requestApiWithType:kLoginRefreshApi andParam:nil];
}


// 邮箱注册或邮箱登录
+ (YFLoginApi *)emailSignInOrSignUp:(BOOL)isSignIn email:(NSString *)email password:(NSString *)password {
    NSDictionary *dict = @{};
    if (isSignIn) {
        dict = @{@"username":[NSString checkIfNullWithString:email],
                 @"password":[NSString checkIfNullWithString:password],
                 @"type":@"T0",
                 @"version":[@"IOS" stringByAppendingString:[NSString appCurrentVersion]],
                 };
    }else {
        dict = @{@"useraccount":[NSString checkIfNullWithString:email],
                 @"pwd":[NSString checkIfNullWithString:password],
                 @"oemType": @"T0",
                 @"register": @"1",
                 };
    }
    RequestApiType apiType = isSignIn ? kLoginEmailSignInApi:kLoginEmailSignUpApi;
    return [YFLoginApi requestApiWithType:apiType andParam:dict];
}

// 手机验证码登录
+ (YFLoginApi *)loginApiByPhone:(NSString *)phoneNumber verifyCode:(NSString *)verifyCode withComplete:(void(^)(NSString *userId, BOOL isFirstLogin,NSError *error))completeBlock {
    NSDictionary *paramdict = @{@"zone": @"86",
                                @"phone": phoneNumber,
                                @"code": verifyCode,
                                @"oemType": @"T0",
                                @"systemVersion": @"IOS",
                                @"appVersion": [NSString appCurrentVersion],
                                };
    YFLoginApi *phoneLoginApi = [YFLoginApi requestApiWithType:kLoginPhoneSignInApi andParam:paramdict];
    [[YFNetworkingService sharedInstance] sendRequest:phoneLoginApi callback:^(BOOL success, id result, NSError *error) {
        if (error == nil) {
            NSString *userId = @"";
            BOOL isFirst = NO;
            if ([[result responseInfo] isKindOfClass:[NSDictionary class]]) {
                userId = [[result responseInfo] objectForKey:@"UserID"];
                isFirst = [[[result responseInfo] objectForKey:@"FirstLogin"] integerValue] == 1;
            }
            if (completeBlock) {
                completeBlock(userId,isFirst,nil);
            }
        }else {
            if (completeBlock) {
                completeBlock(nil,NO,error);
            }
        }
    }];
    return phoneLoginApi;
}

// 找回密码
+ (YFLoginApi *)forgotpasswordWithEmail:(NSString *)email {
    return [YFLoginApi requestApiWithType:kLoginForgotpasswordApi andParam:@{@"email":[NSString checkIfNullWithString:email]}];
}

/**
 * loginType: 平台类型，微信 2， Facebook:5
 * credential：授权凭证
 * token: 授权令牌
 */
+ (instancetype)loginWithThridPartplatform:(NSString *)loginType
                                  credential:(NSString *)credential
                                   token:(NSString *)token {
    
    NSDictionary *paramDict = @{@"type": loginType,
                                @"openId": [NSString checkIfNullWithString:credential],
                                @"token": [NSString checkIfNullWithString:token],
                                @"systemVersion":@"IOS",
                                @"oemType":@"T0",
                                @"appVersion":[NSString appCurrentVersion],
                                };
    
    if ([loginType isEqualToString:@"2"]) {
        return [YFLoginApi requestApiWithType:kLoginByThirdPart andParam:paramDict];
    }else if ([loginType isEqualToString:@"5"]){
        return [YFLoginApi requestApiWithType:kLoginFaceBookApi andParam:paramDict];
    }
    return nil;
}




@end
